package com.gdpu.controller;
import com.gdpu.api.CommonPage;
import com.gdpu.api.CommonResult;
import com.gdpu.mapper.OmsCartItemMapper;
import com.gdpu.model.OmsCartItem;
import com.gdpu.service.OmsCartItemSerivece;
import com.gdpu.vo.OmsCartGoodInfoVo;
import com.gdpu.vo.OmsCartItemVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author:weiming
 * @Date:2021/5/29
 */
@Api(tags = "购物车模块")
@RestController
@RequestMapping("/cart")
public class OmsCartItemController {
    @Autowired
    private OmsCartItemSerivece omsCartItemSerivece;
    @Autowired
    private OmsCartItemMapper omsCartItemMapper;

    @ApiOperation("分页获取购物车商品")
    @GetMapping("/")
    public CommonResult get(@RequestParam(name = "pageNum",defaultValue = "0") Integer pageNum,
                            @RequestParam(name = "pageSize",defaultValue = "10")  Integer pageSize){
        CommonPage<OmsCartGoodInfoVo> list = omsCartItemSerivece.list( pageNum, pageSize);
        if (list == null) {
            return  CommonResult.unauthorized("暂未登录");
        }
        return CommonResult.success(list);
    }


    @ApiOperation("添加商品到购物车")
    @PostMapping("/{goodId}")
    public CommonResult add(@PathVariable(name = "goodId") Integer goodId){
        int count = omsCartItemSerivece.add(goodId);
        if (count > 0){
            return CommonResult.success("添加到购物车成功");
        }

        return CommonResult.failed();
    }


    @ApiOperation("更新购物车中某商品的数量")
    @PutMapping("/")
    public CommonResult update(@RequestParam(name = "id")Integer id,
                               @RequestParam(name = "quantity")Integer quantity){
        int count = omsCartItemSerivece.update(id, quantity);
        if (count > 0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation("删除购物车商品")
    @DeleteMapping("/{cartId}")
    public CommonResult delete(@PathVariable("cartId") Integer cartId){
        int count = omsCartItemSerivece.delete(cartId);
        if (count>0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation("勾选/取消勾选 购物车商品")
    @PutMapping("/check")
    CommonResult check(@RequestParam(name = "isCheck")Integer isCheck,
                       @RequestParam(name = "cartId")Integer cartId){
        int count = omsCartItemSerivece.check(isCheck,cartId);
        if (count > 0){
            return  CommonResult.success("订单取消成功");
        }
        return CommonResult.failed();
    }
}
